package com.zhongke.cotrun.controller;

import com.alibaba.fastjson.JSONException;
import com.github.pagehelper.PageInfo;
import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.exception.MyException;
import com.zhongke.cotrun.model.pojo.SignUpInfo;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.SignUpInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "活动报名信息服务")
@RestController("signUpInfo")
@RequestMapping("/signUpInfo")
public class SignUpInfoController {

    @Autowired
    private SignUpInfoService signUpInfoService;

    /**
     * 保存报名信息
     *
     * @param signUpInfo
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存报名信息", httpMethod = "POST")
    public APIResponse save(SignUpInfo signUpInfo) {
        if (signUpInfoService.save(signUpInfo)) {
            return APIResponse.returnSuccess("操作成功");
        }
        return APIResponse.returnFail("操作失败");
    }

    /**
     * 批量保存报名信息
     *
     * @param list
     * @return
     */
    @PostMapping("/saveBatch")
    @ApiOperation(value = "批量保存报名信息", httpMethod = "POST")
    public APIResponse saveBatch(@ApiParam(value = "报名信息", required = true)@RequestBody List<SignUpInfo> list) throws MyException {
        try {
            if (signUpInfoService.saveBatch(list)) {
                return APIResponse.returnSuccess("操作成功");
            }
        } catch (JSONException e) {
            throw new MyException(CodeMsg.JSON_ERROR);
        }
        return APIResponse.returnFail("操作失败");
    }

    /**
     * 报名统计
     *
     * @param activitiesId
     * @return
     */
    @GetMapping("/getSignUpInfoList")
    @ApiOperation(value = "报名统计", httpMethod = "POST")
    public APIResponse getSignUpInfoList(@ApiParam(value = "活动id", required = true)Integer activitiesId,
                                     @ApiParam(value = "当前页数,默认为1", required = true)@RequestParam(defaultValue = "1") Integer pageNum,
                                     @ApiParam(value = "每页显示条数,默认为10", required = true)@RequestParam(defaultValue = "10")Integer pageSize) {
        PageInfo info = signUpInfoService.getSignUpInfo(activitiesId,pageNum,pageSize);
        if (info!=null) {
            return APIResponse.returnSuccess(info);
        }
        return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
    }

    /**
     * 根据订单号查看报名信息
     *
     * @param orderCode
     * @return
     */
    @GetMapping("/getSignUpInfoByOrderCode")
    @ApiOperation(value = "根据订单号查看报名信息", httpMethod = "GET")
    public APIResponse getSignUpInfoByOrderCode(@ApiParam(value = "订单号", required = true)String orderCode,
                                         @ApiParam(value = "当前页数,默认为1", required = true)@RequestParam(defaultValue = "1") Integer pageNum,
                                         @ApiParam(value = "每页显示条数,默认为10", required = true)@RequestParam(defaultValue = "10")Integer pageSize) {
        PageInfo info = signUpInfoService.getSignUpInfoByOrderCode(orderCode,pageNum,pageSize);
        if (info!=null) {
            return APIResponse.returnSuccess(info);
        }
        return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
    }

}
